package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbSts;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbStsRepository extends JpaRepository<CmdbSts, Integer>, JpaSpecificationExecutor<CmdbSts> {
    @Query(value = "select m from CmdbSts m where m.isDelete=0 " +
            "and (:#{#paramSearchDto.stsCode} is null OR m.stsCode LIKE CONCAT('%', :#{#paramSearchDto.stsCode},'%') ) " +
            "and (:#{#paramSearchDto.sn} is null OR m.sn LIKE CONCAT('%',:#{#paramSearchDto.sn},'%') ) " +
            "and (:#{#paramSearchDto.machineCabinetUBusinessCode} is null OR m.machineCabinetUBusinessCode= :#{#paramSearchDto.machineCabinetUBusinessCode}) " +
            "and (:#{#paramSearchDto.elePortAmount} is null OR m.elePortAmount= :#{#paramSearchDto.elePortAmount}) " +
            "and (:#{#paramSearchDto.elePortUseAmount} is null OR m.elePortUseAmount= :#{#paramSearchDto.elePortUseAmount}) " +
            "and (:#{#paramSearchDto.machineCabinetBusinessCode} is null OR m.machineCabinetBusinessCode= :#{#paramSearchDto.machineCabinetBusinessCode}) " +
            "")
    Page<CmdbSts> searchByParam(Pageable pageable, CmdbSts paramSearchDto);

    List<CmdbSts> findAllByIdIn(List<Long> id);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_sts set is_delete = :deleted where sts_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);
}



